package com.score.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.score.mapper.IDormMapper;
import com.score.mapper.IStudentMapper;
import com.score.mapper.ITeacherMapper;
import com.score.pojo.*;
import com.score.service.impl.StudentServiceImpl;
import com.score.service.impl.TeacherServiceImpl;
import com.score.service.impl.UserServiceImpl;
import com.score.util.PinyinUtils;
import net.coobird.thumbnailator.Thumbnails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
public class InfoController {
    @Autowired
    private StudentServiceImpl studentService;

    @Autowired
    private TeacherServiceImpl teacherService;

    @Autowired
    private UserServiceImpl userService;

    @Autowired
    private IStudentMapper studentMapper;

    @Autowired
    private ITeacherMapper teacherMapper;

    @Autowired
    private IDormMapper dormMapper;

    @Secured("ROLE_headmaster")
    @GetMapping("/teaCheckDorm")
    public ReturnEntry teaCheckDorm(String username) {
        QueryWrapper<TTeacher> teacherQueryWrapper = new QueryWrapper<>();
        QueryWrapper<TDorm> dormQueryWrapper = new QueryWrapper<>();
        teacherQueryWrapper.eq("num",username);
        TTeacher tTeacher = teacherMapper.selectOne(teacherQueryWrapper);
        dormQueryWrapper.eq("class_name",tTeacher.getClassName());
        List<TDorm> dormList = dormMapper.selectList(dormQueryWrapper);
        return new ReturnEntry().ok(dormList,"班主任查寝");
    }

    @PostMapping("/stuInfo")
    public ReturnEntry sendStuInfo(String stunum) {
        TStudent student = studentService.getStudentInfo(stunum);
        student.setCollegeAbbreviation(PinyinUtils.getHeadPinYin(student.getCollege()));
        studentMapper.updateById(student);
        return new ReturnEntry().ok("学生基础信息", student, PinyinUtils.getStringAndInt(student.getClassName()), "");
    }

    @Secured({"ROLE_headmaster", "ROLE_instructor", "ROLE_deputySecretary", "ROLE_dean"})
    @PostMapping("/teaInfo")
    public ReturnEntry sendTeaInfo(String teanum) {
        TTeacher teacherInfo = teacherService.getTeacherInfo(teanum);
        teacherInfo.setCollegeAbbreviation(PinyinUtils.getHeadPinYin(teacherInfo.getCollege()));
        teacherMapper.updateById(teacherInfo);
        return new ReturnEntry().ok("老师基础信息", teacherInfo, PinyinUtils.getStringAndInt(teacherInfo.getClassName()), "");
    }

    @Secured({"ROLE_instructor", "ROLE_headmaster", "ROLE_student", "ROLE_stucadres", "ROLE_deputySecretary", "ROLE_dean"})
    @PostMapping("/changePasswd")
    public ResponseEntry changePasswd(String username, String oldPasswd, String newPasswd) {
        TUser tUser = userService.findByName(username);
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        boolean matches = encoder.matches(oldPasswd, tUser.getPassword());
        if (matches) {
            String enPasswd = encoder.encode(newPasswd);
            Integer integer = userService.changePasswd(username, enPasswd);
            if (integer == 1)
                return new ResponseEntry().ok("修改成功", null);
            return new ResponseEntry().fail("修改失败", null);
        } else {
            return new ResponseEntry().fail("原用户名密码错误", null);
        }

    }
    @Secured({"ROLE_instructor", "ROLE_headmaster", "ROLE_student", "ROLE_stucadres", "ROLE_deputySecretary", "ROLE_dean"})
    @PostMapping("/tupian")
    public ResponseEntry sendTupian(MultipartFile file) throws IOException {
        //定义上传文件服务器路径
        String path = "D:/web/scoreweb/scorePicture";
        //获取上传文件名
        String filename = file.getOriginalFilename();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        //将文件名设为唯一值
        filename = uuid + filename.substring(filename != null ? filename.lastIndexOf(".") : 0);
        Thumbnails.of(file.getInputStream()).scale(0.5f)
                .toFile(path+"/"+filename);
        return new ResponseEntry().ok("上传成功", "http://cxcy.gmu.cn/gmuscore/scorePicture/" + filename);
    }

    @Secured("ROLE_student")
    @PostMapping("/sendFiveOne")
    public ResponseEntry sendFiveOne(MultipartFile file) throws IOException {
        //定义上传文件服务器路径
        String path = "D:/web/scoreweb/fiveone";
        //获取上传文件名
        String filename = file.getOriginalFilename();
        //将文件名设为唯一值
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        filename = df.format(new Date()) + "_" + filename;
        file.transferTo(new File(path, filename));
        File file1 = new File("D:/web/scoreweb/fiveone/" + filename);
        if (file1.exists())
            return new ResponseEntry().ok("上传成功", "http://cxcy.gmu.cn/gmuscore/fiveone/" + filename);
        else
            return new ResponseEntry().fail("上传失败", null);
    }


}
